# 运营商:
# @telecom(中国电信)
# @cmcc(中国移动)
# @unicom(中国联通)
# 这里需要把@换为%40
# 注意，连接WiFi需要安装pywifi库和comtypes库

import requests
import time
import pywifi


def WiFi_Connect():
    ssid = "CUMT_Stu"
    wifi = pywifi.PyWiFi()
    ifaces = wifi.interfaces()
    iface = ifaces[0]
    status = iface.status()
    if status != pywifi.const.IFACE_CONNECTED:
        profile = pywifi.Profile()
        profile.ssid = ssid
        iface.connect(profile)
        time.sleep(3)
        if iface.status() == pywifi.const.IFACE_CONNECTED:
            print("WiFi连接成功!")
        else:
            print("WiFi连接失败!")


def WiFi_Login():
    url = "http://9.9.9.9"
    response = requests.get(url, allow_redirects=False)
    url = response.url
    t = url.find("wlanuserip=")
    ip = url[t + 11:url.find("&", t + 11, t + 24)]
    t = url.find("wlanacname=")
    wlanacname = url[t + 11:url.find("&", t + 11)]
    t = url.find("mac=")
    mac = url[t + 4:]
    mac = "".join(mac.split("-"))
    loginurl = "10.2.5.251:801"
    user_account = "学号"
    user_password = "密码"
    ISP_select = "运营商"
    login = "http://" + loginurl + "/eportal/?c=Portal&a=login&login_method=1&user_account=" + user_account + ISP_select + "&user_password=" + user_password + "&wlan_user_ip=" + ip + "&wlan_user_mac=" + mac + "&wlan_ac_ip=&wlan_ac_name=" + wlanacname + "&jsVersion=3.0"
    resault = requests.get(login)
    if resault.status_code == 200:
        print("登录成功!")
    else:
        print("登录失败!")


if __name__ == "__main__":
    WiFi_Connect()  # 连接WiFi
    WiFi_Login()  # 登录
